// formPackage/pages/platformModule/platformPayPage/platformPayPage.js


import { openMemberPay, memberPaySetting } from "../../../../service/platform_service"
import debounce from "../../../../tools/debounce";

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    topBgPath: "https://saas.img.ylyc.top/bg/enteragentpaybg_icon.png",
    companyBgPath: "https://saas.img.ylyc.top/bg/entercompanypaybg_icon.png",
    agencyBgPath: "https://saas.img.ylyc.top/bg/enteragentpaybg_icon.png",
    type: "agent",
    payId: "",
    payType: 1,//1:经纪人开通，2:公司开通
    showSuccessPop: false,
    remindText: "支付成功，待管理员审核资料后生效",
    payMoney: 0,
    isAgreeProtocol: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("JJLog:传过来的options:" + JSON.stringify(options));
    this.setData({
      type: options.type,
      payId: options.id
    })
    if (this.data.type == "agent") {
      this.setData({
        topBgPath: this.data.agencyBgPath,
        payType: 1
      })
    } else {
      this.setData({
        topBgPath: this.data.companyBgPath,
        payType: 2
      })
    }
    this.memberPaySetting();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //获取支付配置:
  memberPaySetting() {

    memberPaySetting(this.data.payType).then(res => {
      console.log("JJLog:支付配置是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          payMoney: res.data.price
        })
      }
    }).catch(err => {
      console.error("JJLog:memberPaySetting", err);
    })
  },


  //确认开通会员
  sureOpenMemberAction() {
    if (!this.data.isAgreeProtocol) {
      wx.showToast({
        title: '请输入同意用户政策与隐私',
        icon: "none"
      })
      return;
    }
    debounce(this.openMemberPay)
  },


  //开通会员支付操作:
  openMemberPay() {
    let remindText = "支付成功，待管理员审核资料后生效";
    if (this.data.payType == 2) {
      remindText = "支付成功，待管理员审核资料后生效";
    }
    this.setData({
      remindText: remindText
    })
    wx.showLoading();
    openMemberPay(this.data.payType, this.data.payId).then(res => {
      wx.hideLoading();
      console.log("JJLog:支付操作:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data.isFree) {
          this.setData({
            showSuccessPop: true
          })
          return;
        }
        let payData = res.data.data;
        wx.requestPayment({
          timeStamp: payData.timeStamp,
          nonceStr: payData.nonceStr,
          package: payData.packageValue,
          signType: 'MD5',
          paySign: payData.paySign,
          success: (res) => {
            wx.hideLoading();
            console.log("JJLog:微信支付成功回调:" + JSON.stringify(res));
            this.setData({
              showSuccessPop: true
            })
            // wx.showModal({
            //   title: '提示',
            //   content: remindText,
            //   showCancel: false,
            //   success: res => {
            //     if (res.confirm) {
            //       this.setData({
            //         showInputPop: false,
            //         formData: emptyData
            //       })
            //     }
            //   }
            // })


          },
          fail: (err) => {
            wx.hideLoading();
            wx.showToast({
              title: '支付失败',
              icon: "none"
            })
            console.log("JJLog:微信支付失败回调:" + JSON.stringify(err));
          }
        })

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:openMemberPay", err);
    })
  },
  /**
  * 协议操作
  */
  agreenAction() {
    this.setData({
      isAgreeProtocol: !this.data.isAgreeProtocol
    })
  },
  successPopSure() {
    this.setData({
      showSuccessPop: false,
    })
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

})